const express = require('express')
const router = express.Router()
const db = require('../../connection/Connection')
const jwt = require('jsonwebtoken')
const moment = require('moment')
const SECRET = 'asdfta219*&%^jWJSHFJ'


router.post('/addEmployee', async (req, res) => {
    console.log(req.body)
    //token
    const result = jwt.verify(req.body.token, SECRET)
    let sql = 'select departmentId from user_role where username = ?'
    db.base(sql, result.username, response_temp => {
        console.log(req.body)
        //     req.body.contractStart = req.body.contractStart.replace("Z", " UTC");//是空格+UTC
        req.body.contractStart = moment(req.body.contractStart).format('YYYY-MM-DD HH:mm:ss')
        req.body.contractEnd = moment(req.body.contractEnd).format('YYYY-MM-DD HH:mm:ss')
        console.log(req.body)
        if (response_temp != null && response_temp.length == 1) {
            sql = 'CALL addEmployee(?,?,?,?,?,?,?,?,?,?);'
            let data = [
                req.body.salary,
                req.body.name,
                req.body.age,
                req.body.gender,
                req.body.address,
                req.body.education,
                req.body.contact,
                req.body.stationName,
                req.body.contractStart,
                req.body.contractEnd
            ]
            db.base(sql, data, response_temp => {
                console.log(response_temp)
                if (response_temp != null&& response_temp.affectedRows == 1) {
                    res.json({
                        result: "success"
                    })
                } else {
                    res.json({
                        result: "error"
                    })
                }
            })

            // sql = 'INSERT INTO salary  (salary_basic,overtime_wage,bonus,deduction_wages) VALUES (?,0,0,0);'
            // db.base(sql, req.body.salary, response => {
            //     console.log(response)
            //     if (response!= null &&response.affectedRows == 1) {
            //         sql = 'INSERT INTO employees (`name`,age,gender,address,education,contact,station_id,attendance_id,salary_id,train_id,contract_start,contract_end) VALUES (?,?,?,?,?,?,?,(SELECT attendance_id from attendance ORDER BY attendance_id DESC LIMIT 0,1),(SELECT salary_id FROM salary ORDER BY salary_id DESC LIMIT 0,1),(SELECT train_id FROM train ORDER BY train_id DESC LIMIT 0,1),?,?)'
            //         let data = [
            //             req.body.name,
            //             req.body.age,
            //             req.body.gender,
            //             req.body.address,
            //             req.body.education,
            //             req.body.contact,
            //             req.body.stationName,
            //             req.body.contractStart,
            //             req.body.contractEnd
            //         ]
            //         db.base(sql, data, response => {
            //             console.log(response)
            //             if (response!= null &&response.affectedRows == 1) {
            //                 res.json({
            //                     result: "success"
            //                 })
            //             } else {
            //                 res.json({
            //                     result: "error"
            //                 })
            //             }
            //         })
            //     } else {
            //         res.json({
            //             result: "error"
            //         })
            //     }
            // })
        } else {
            res.json({
                result: "error"
            })
        }

    })

})

module.exports = router
